/*******************************************************************************
 * StandardHistoryManager.cpp
 *
 * Copyright (c) 2012, Varuna L Amachi. All rights reserved.
 *
 * This program/library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This program/library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301  USA
 ******************************************************************************/

#include "StandardHistoryManager.h"
#include "core/manager/T.h"

#include <QSize>
#include <QFont>

using namespace Tanyatu;

using namespace Component;

QString StandardHistoryManager::COMPONENT_NAME =
        QString( "Tanyatu::Component::StandardHistoryManager" );

QString StandardHistoryManager::COMPONENT_MODULE =
        QString( "Tanyatu::Core" );


StandardHistoryManager::StandardHistoryManager( QObject *parent ) :
    AbstractHistoryManager( parent )
{

}


bool StandardHistoryManager::init()
{
    //Here we clear all the history erlier than 10 days before
    QDateTime from = QDateTime::currentDateTime().addDays( -10 );
    DATA_UPDATER()->clearHistory( from );

    //Get the stuff from last 10 days
    return DATA_RETRIEVER()->getHistory( m_history );
}


void StandardHistoryManager::onItemPlayed( Tanyatu::Model::MediaItem *item )
{
    if( item->type() == Tanyatu::Model::Media_StoredAudio
            || item->type() == Model::Media_StoredVideo ) {
        QDateTime currentTime = QDateTime::currentDateTime();
        DATA_UPDATER()->addHistory( item->trackId(),
                                             currentTime );
        QPair< QString, QDateTime > *history =
                new QPair< QString, QDateTime >( item->trackId(), currentTime );
        beginInsertRows( QModelIndex(),
                         m_history.isEmpty() ? 0 : m_history.size() - 1,
                         m_history.isEmpty() ? 0 : m_history.size() - 1);
        m_history.append( history );
        endInsertRows();
    }
}


void StandardHistoryManager::clear()
{
    beginResetModel();
    DATA_UPDATER()->clearHistory();
    clearStruct();
    endResetModel();
}

void StandardHistoryManager::getHistoryItemIds( QList<QString> &ids ) const
{
    QPair< QString, QDateTime > *history = 0;
    foreach( history, m_history ) {
        ids.append( history->first );
    }
}


int StandardHistoryManager::rowCount( const QModelIndex &parent ) const
{
    Q_UNUSED( parent )
    return m_history.size();
}

int StandardHistoryManager::columnCount(const QModelIndex &parent) const
{
    Q_UNUSED( parent );
    return 5;
}


QVariant StandardHistoryManager::data(const QModelIndex &index, int role) const
{
    if( ! index.isValid() || index.row() >= m_history.size() ) {
        return QVariant();
    }
    if( role == Qt::DisplayRole ) {
        QPair< QString, QDateTime > *history = m_history.at( index.row() );
        Model::StoredAudio *track = T::get()->audioLib()->item(
                    history->first );
        switch( index.column() ) {
        case 0: return track->id();
        case 1: return track->title();
        case 2: return track->artist();
        case 3: return track->album();
        case 4: return history->second.toString();
        }
    }
    else if( role == Qt::TextAlignmentRole ) {
        return int( Qt::AlignLeft | Qt::AlignVCenter );
    }
    else if( role == Qt::SizeHintRole ) {
        return QSize( 0, 18 );
    }
    else if( role == Qt::FontRole ) {
        return QFont( "Droid Sans", 9 );
    }
    return QVariant();
}

QVariant StandardHistoryManager::headerData(
        int section, Qt::Orientation orientation, int role) const
{
    Q_UNUSED( orientation );
    if( role == Qt::DisplayRole ) {

        switch( section ) {
        case 0: return tr("id");
        case 1: return tr("Title");
        case 2: return tr("Artist");
        case 3: return tr("Album");
        case 4: return tr("Time");
        }
    }
    return QVariant();
}


QModelIndex StandardHistoryManager::index(
        int row, int column, const QModelIndex &parent) const
{
    Q_UNUSED( parent )
    return createIndex( row, column );
}


QModelIndex StandardHistoryManager::parent(const QModelIndex &child) const
{
    Q_UNUSED( child );
    return QModelIndex();
}


bool StandardHistoryManager::hasChildren(const QModelIndex &parent) const
{
    if( ! parent.isValid() ) {
        return true;
    }
    return false;
}


QString StandardHistoryManager::uniqueName() const
{
    return COMPONENT_NAME;
}


QString StandardHistoryManager::module() const
{
    return COMPONENT_MODULE;
}


QString StandardHistoryManager::displayName() const
{
    return "History";
}



void StandardHistoryManager::clearStruct()
{
    QPair< QString, QDateTime > *history = 0;
    foreach( history, m_history ) {
        delete history;
    }
    m_history.clear();
}




void StandardHistoryManager::onItemRemovalFromStorage( QString trackId )
{
    for( int i = m_history.size() - 1; i >= 0; -- i ) {
        QString itemId = m_history[ i ]->first;
        if( itemId == trackId ) {
            m_history.removeAt( i );
            DATA_UPDATER()->removeHistoryItem( trackId );
            return;
        }
    }
}
